#include "particlesystem.h"
#include <QRandomGenerator>
#include <QDebug>
ParticleSystem::ParticleSystem()
{

}

ParticleSystem::~ParticleSystem()
{

}

void ParticleSystemInfo::AddTexture(QString path)
{
    QOpenGLTexture *texture = new QOpenGLTexture(QImage(path).mirrored(),QOpenGLTexture::GenerateMipMaps);
    texture->setWrapMode(QOpenGLTexture::Repeat);
    texture->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::Repeat);
    texture->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::Repeat);
    texture->setMinificationFilter(QOpenGLTexture::Nearest);
    texture->setMagnificationFilter(QOpenGLTexture::Linear);
    texs.push_back(texture);
    texpath.push_back(path);
}

void ParticleSystemInfo::DeleteTexture(int i)
{
    if(texs.count() <= i || i < 0 || texs.count() == 1)
        return;
    QOpenGLTexture* t = texs[i];
    texs.remove(i);
    delete t;
    t = nullptr;
    texpath.remove(i);
}

QOpenGLTexture *ParticleSystemInfo::GetTexture(int index)
{
    if(index < 0)
        return nullptr;
    if(index >= texs.count())
        return texs[0];
    return texs[index];
}

QString ParticleSystemInfo::GetPath(int i)
{
    return texpath[i];
}

int ParticleSystemInfo::TextureCount()
{
    return texs.count();
}

Particle *ParticleSystem::GetParticle()
{
    if(info->TextureCount() <= 0)
        return nullptr;
    Particle* p = nullptr;
    if(pool.count() > 0)
    {
        p = pool.back();
        pool.pop_back();
    }
    if(!p)
        p = new Particle();

    //刷新随机属性
    info->RefreshParticle(p);

    return p;
}

void ParticleSystem::ReturnParticle(Particle *p)
{
    if(pool.count() <= MAX_COUNT)
        pool.push_back(p);
    else
    {
        delete p;
        p = nullptr;
    }
}

ParticleSystemInfo::ParticleSystemInfo()
{

}

ParticleSystemInfo::~ParticleSystemInfo()
{

}

bool ParticleSystemInfo::SetInitPosRange(float min_x, float min_y, float max_x, float max_y)
{
    if(min_x > max_x ||min_y > max_y)
        return false;
    min_x0 = min_x;
    min_y0 = min_y;
    max_x0 = max_x;
    max_y0 = max_y;
    return true;
}

bool ParticleSystemInfo::SetVRange(float min_x, float min_y, float max_x, float max_y)
{
    if(min_x > max_x ||min_y > max_y)
        return false;
    min_xv = min_x;
    min_yv = min_y;
    max_xv = max_x;
    max_yv = max_y;
    return true;
}

bool ParticleSystemInfo::SetARange(float min_x, float min_y, float max_x, float max_y)
{
    if(min_x > max_x ||min_y > max_y)
        return false;
    min_xa = min_x;
    min_ya = min_y;
    max_xa = max_x;
    max_ya = max_y;
    return true;
}

bool ParticleSystemInfo::SetSizeRange(float minx, float maxx, float miny, float maxy)
{
    if(minx > maxx || miny > maxy)
        return false;
    min_xsize = minx;
    min_ysize = miny;
    max_xsize = maxx;
    max_ysize = maxy;
    return true;
}

void ParticleSystemInfo::RefreshParticle(Particle *p)
{
    p->texIndex = QRandomGenerator::global()->bounded(0,texs.count());

    p->x0 = QRandomGenerator::global()->bounded(max_x0 - min_x0) + min_x0;
    p->y0 = QRandomGenerator::global()->bounded(max_y0 - min_y0) + min_y0;
    p->z0 = QRandomGenerator::global()->bounded(max_z0 - min_z0) + min_z0;

    p->xv = QRandomGenerator::global()->bounded(max_xv - min_xv) + min_xv;
    p->yv = QRandomGenerator::global()->bounded(max_yv - min_yv) + min_yv;
    p->zv = QRandomGenerator::global()->bounded(max_zv - min_zv) + min_zv;

    p->xa = QRandomGenerator::global()->bounded(max_xa - min_xa) + min_xa;
    p->ya = QRandomGenerator::global()->bounded(max_ya - min_ya) + min_ya;
    p->za = QRandomGenerator::global()->bounded(max_za - min_za) + min_za;

    p->xsize = QRandomGenerator::global()->bounded(max_xsize - min_xsize) + min_xsize;
    p->ysize = QRandomGenerator::global()->bounded(max_ysize - min_ysize) + min_ysize;

    if(lockScale)
        p->ysize = p->xsize;
}
